<template>
  <div class="container">
    <el-form ref="form" :data='row' label-width="80px">
      <el-dialog :title="title" :before-close="handleClose" :visible.sync="dialogVisible" width="30%">
        <el-form-item label="学科名称">
          <el-input v-model="row.subjectName" placeholder="学科名称"></el-input>
        </el-form-item>
        <el-form-item label="是否显示">
          <el-switch v-model="row.isFrontDisplay" active-color="#13ce66" inactive-color="#ff4949" :active-value="1" :inactive-value="0"></el-switch>
        </el-form-item>
        <span slot="footer" class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="addList">确 定</el-button>
        </span>
      </el-dialog>
    </el-form>

  </div>
</template>

<script>
import { update, add } from '@/api/hmmm/subjects.js'
// import { questionType } from '@/api/hmmm/constants.js'
export default {
  name: 'subjectadd',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    row: {
      type: Object
      // required: true

    },
    type: {
      type: String

    }
  },
  data () {
    return {
      // isShow: {
      //   ok: 1,
      //   no: 0
      // }
      // list: {
      //   subjectName: '',
      //   isFrontDisplay: ''
      // }
    }
  },
  methods: {
    handleClose () {
      this.$emit('update:dialogVisible', false)
      // this.$refs.form.resetFields()
      this.$emit('Kong')
    },

    // 修改学科信息
    async addList () {
      if (this.type === 'upd') {
        // 调用修改接口
        const arr = await update(this.row)
        // 修改数据
        if (arr.status === 200) {
          console.log('我很好很好，', arr)
          this.$message.success('修改学科成功')
          this.$emit('update:dialogVisible', false)
          this.row.subjectName = ''
          // this.$refs.form.resetFields()//没有验证规则,不生效
          this.$parent.loadRoleList()
        } else {
          this.$message.error('修改学科信息失败')
        }
      } else {
        // 调用新增接口
        const addr = await add(this.row)
        if (addr.status === 200) {
          console.log(addr, '很好很好')
          this.$message.success('新增学科成功')
          this.$emit('update:dialogVisible', false)
          // 重置表单
          this.row.subjectName = ''
          // this.isShow = this.isShow.no
          this.$parent.loadRoleList()
        } else {
          console.log('新增学科失败')
        }
      }
    }
  },
  computed: {
    title () {
      return this.type === 'upd' ? '修改学科' : '新增学科'
    }
  }
}
</script>

<style scoped lang="less"></style>
